﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Util;
using Kingdee.BOS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.Orm.DataEntity;

namespace LiMinCRMPlugin
{
    [Description("其他应付单审核时，校验是否存在付款单")]
    [HotUpdate]
    public class OtherPayUNAuditService : AbstractOperationServicePlugIn
    {

        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            e.FieldKeys.Add("FBILLNO");//单据编号
            e.FieldKeys.Add("F_DEV_OAId");//OA唯一码
            e.FieldKeys.Add("F_DEV_DataSrc");//数据来源

        }
        public override void BeginOperationTransaction(BeginOperationTransactionArgs e)
        {
            base.BeginOperationTransaction(e);

            foreach (DynamicObject entity in e.DataEntitys)
            {
                string F_DEV_DataSrc = Convert.ToString(entity["F_DEV_DataSrc"]);
                if (F_DEV_DataSrc.Equals("OA"))
                {
                    string BillNo = Convert.ToString(entity["BillNo"]);//单据编号                      
                    string F_DEV_OAId = Convert.ToString(entity["F_DEV_OAId"]);//OA唯一码
                    string sql = string.Format(@"select *from T_AP_PAYBILL where F_DEV_OAID='" + F_DEV_OAId + "' and F_DEV_DATASRC='OA' ");
                    DataTable sql_toStirng = DBUtils.ExecuteDataSet(this.Context, sql).Tables[0];
                    if (sql_toStirng.Rows.Count > 0)
                    {

                        string fbillno = sql_toStirng.Rows[0]["FBILLNO"].ToString();
                        string message = "其他应付单[" + BillNo + "]已经下推付款单[" + fbillno + "],反审核失败";
                        throw new KDBusinessException("", message);//强制终止操作，回滚事务
                    }
                }

            }
        }
    }
}
